/// <reference path="../../apicloud.d.ts" />
const vm = new Vue({
    el: '#main',
    data: {
        invitationCode: '',
        shareBoxShow: false,
        nickname: '',
        avatar: '',
        userList: [],
    },
    methods: {
        shareQQ,
        shareWX,
        shareWeiBo,
    },
});

class Share {
    constructor({
        shareUrl,
        succeedMsg = '分享成功',
        title,
        description,
        thumb,
    }) {
        this.url = shareUrl;
        this.succeedMsg = succeedMsg;
        this.title = title;
        this.description = description;
        this.thumb = thumb;
    }

    shareWX(type) {
        vm.shareBoxShow = false;
        const wx = api.require('wx');
        const self = this;
        wx.shareWebpage({
                scene: type,
                title: this.title,
                description: this.description,
                thumb: this.thumb,
                contentUrl: this.url,
            },
            function (ret, err) {
                if (ret.status) {
                    toast(self.succeedMsg);
                }
            },
        );
    }

    shareQQ(type) {
        vm.shareBoxShow = false;
        const qq = api.require('qq');
        const self = this;
        qq.shareNews({
                url: this.url,
                title: this.title,
                description: this.description,
                imgUrl: this.thumb,
                type,
            },
            ret => {
                if (ret.status) {
                    toast(self.succeedMsg);
                }
            },
        );
    }

    shareWeiBo() {
        vm.shareBoxShow = false;
        const weibo = api.require('weibo');
        const self = this;
        weibo.shareWebPage({
                text: this.description,
                title: this.title,
                description: this.description,
                thumb: this.thumb,
                contentUrl: this.url,
            },
            function (ret, err) {
                if (ret.status) {
                    toast(self.succeedMsg);
                }
            },
        );
    }
}

const share = new Share({
    shareUrl: encodeURI(`http://192.168.0.11:8080/share-discounts/dist/index.html?name=${vm.nickname}&avatar=${vm.avatar}&invitationCode=${vm.invitationCode}`),
    title: '分享家短租，注册就送200元住宿基金！',
    description: '分享家短租，注册就送200元住宿基金！',
    thumb: 'http://appv2.fxj178.com/Public/Home/110.png',
});

if (fxj.deBug) {
    main()
}

function main() {
    fxj.fixLayout();
    const main = document.getElementById('main');
    main.style.maxHeight = screen.availHeight - document.querySelector('header').clientHeight + 'px';
    main.style.minHeight = main.style.maxHeight;
    draw();
    reqInvitationCode();
}

function apiready() {
    main();
}

function shareQQ(type) {
    share.shareQQ(type);
}

function shareWX(type) {
    share.shareWX(type);
}

function shareWeiBo() {
    share.shareWeiBo();
}

function draw() {
    const canvas = document.getElementById('canvas');
    const width = screen.availWidth;
    canvas.width = width;
    canvas.height = width / 5 * 3;
    const ctx = canvas.getContext('2d');
    ctx.arc(width / 2, -width / 2 * 3, width * 2, 0, Math.PI);
    const gradient = ctx.createLinearGradient(0, 0, width, 0);
    gradient.addColorStop(0, '#ff5100');
    gradient.addColorStop(1, '#ff8e00');
    ctx.fillStyle = gradient;
    ctx.fill();
}

async function alterInvitationCode() {
    const iptCode = await fxj.dialog.prompt({
        title: '自定义邀请码',
        type: 'text',
        buttons: ['取消', '保存'],
        msg: '8位字母或数字',
    });
    if (iptCode) {
        const res = await ajax({
            url: 'home/userCenter/updateCode',
            data: {
                code: iptCode,
            }
        })
        toast(res.msg)
        if (res.err === 0) {
            vm.invitationCode = iptCode;
        }
    }
}

async function reqInvitationCode() {
    const res = await ajax({
        url: 'home/userCenter/getInvitInfo',
    })
    if (res.err !== 0) {
        toast(res.msg);
        return;

    }
    vm.invitationCode = res.data.code;
    vm.nickname = res.data.me.nickname;
    vm.avatar = res.data.me.photo;
    forEach(res.data.user, user => {
        user.creat_time *= 1000
        user.creat_time = new Date(user.creat_time).toLocaleDateString().replace(/\//g, '.');
    });
    vm.userList = res.data.user;
}